package id.co.shuba.spamobile.data;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.model.FlagFreezeInfo;
import id.co.shuba.spamobile.model.StandardInfo;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FlagFreezeDB extends BaseDB {		
	
	public FlagFreezeDB(DatabaseHandler db) {
		super(db);
	}
			
	public static void addOrUpdate(SQLiteDatabase db, int flag, String field, String enabled, String date, String isActive) {
		String[] params = new String[] { String.valueOf(flag), field, enabled, date, isActive };
		db.execSQL("INSERT OR REPLACE INTO TBL_MS_FLAG_FREEZE(FLAG, FIELD, ENABLED, UPDATE_DATE, IS_ACTIVE) VALUES(?,?,?,?,?)", params);		
	}	
		
	public List<FlagFreezeInfo> getList(int flag) {
		String sql = "SELECT FLAG, FIELD, ENABLED FROM TBL_MS_FLAG_FREEZE WHERE IS_ACTIVE = '1' AND FLAG = " + String.valueOf(flag);		

		SQLiteDatabase db = _db.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);

		List<FlagFreezeInfo> list = new ArrayList<FlagFreezeInfo>();

		if (cursor.moveToFirst()) {
			do {
				FlagFreezeInfo info = new FlagFreezeInfo();
				info.setFlag(cursor.getInt(0));
				info.setField(cursor.getString(1));
				info.setEnabled(cursor.getString(2));
				
				list.add(info);
			} while (cursor.moveToNext());
		}

		cursor.close();

		return list;
	}
	
	public static String getMaxDate(SQLiteDatabase db) {		
		Cursor cursor = db.rawQuery(
				"SELECT MAX(UPDATE_DATE) UPDATE_DATE FROM TBL_MS_FLAG_FREEZE", null);

		if (cursor != null && cursor.getCount() > 0)
			cursor.moveToFirst();
		else {
			cursor.close();
			return null;
		}
		
		String result = cursor.getString(cursor.getColumnIndex("UPDATE_DATE"));		
		cursor.close();

		return result;
	}	
}
